package com.translate.commons.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * 给自定义的线程用的依赖，如果选用springboot的就不用这个
 * src/main/java/com/translate/commons/asynctask，这个包下是自定义的
 */
public class SpringContextHelper implements ApplicationContextAware {
    private static ApplicationContext applicationContext;

    public Object getBean(String beanName) {
        return applicationContext.getBean(beanName);
    }

    public Object getBean(Class className) {
        return applicationContext.getBean(className);
    }

    @Override
    public void setApplicationContext(ApplicationContext _applicationContext) throws BeansException {

        applicationContext = _applicationContext;
    }

    public static ApplicationContext getApplicationContext() {
        if (applicationContext == null) {
            throw new NullPointerException("applicationContext is null   = =!");
        }
        return applicationContext;
    }
}
